package com.example.yichuangyun.mvvm.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ActivityOutsideInvestBinding;
import com.example.yichuangyun.model.company.CompanyLabelModel;
import com.example.yichuangyun.model.company.OutsideInvestDataModel;
import com.example.yichuangyun.mvvm.base.BaseActivity;
import com.example.yichuangyun.mvvm.ui.activity.view.OutsideInvestViewModel;
import com.example.yichuangyun.mvvm.ui.adapter.InfoTabAdapter;
import com.example.yichuangyun.mvvm.ui.adapter.OutsideInvestAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 对外投资
 */
@SuppressLint("NotifyDataSetChanged")
public class OutsideInvestActivity extends BaseActivity<ActivityOutsideInvestBinding, OutsideInvestViewModel> {

//    private final List<CompanyLabelModel> tabData = new ArrayList<>();
//    private InfoTabAdapter tabAdapter;
    private final List<OutsideInvestDataModel> data = new ArrayList<>();
//    private int mPos;
    private LinearLayoutManager manager;

    @Override
    protected OutsideInvestViewModel bindModel() {
        return getViewModel(this, OutsideInvestViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        initTitleBar(getString(R.string.dwtz));
//        tabData.add(new CompanyLabelModel(getString(R.string.jyyc)));
//        data.add(new OutsideInvestDataModel(getString(R.string.jyyc), 1, false));
        data.add(new OutsideInvestDataModel(getString(R.string.dwtz), 1, true));

        manager = new LinearLayoutManager(this);
        binding.jyycRecyclerView.setLayoutManager(manager);

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            String eid = bundle.getString(Constant.eidKey);
//            int position = bundle.getInt(Constant.positionKey);
//            tabData.get(position).setSelect(true);
//            data.get(position).setOpen(true);
//            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) binding.recyclerView.getLayoutManager();
//            if (null != linearLayoutManager) {
//                linearLayoutManager.scrollToPositionWithOffset(position, 0);
//            }
            viewModel.getOutsideInvestInfo(eid);
        }
    }

    @Override
    protected void initClick() {
        viewModel.listMutableLiveData.observe(this, outsideInvestModels -> {
            if (outsideInvestModels != null && outsideInvestModels.size() > 0) {
                data.get(0).setList(outsideInvestModels);
//                tabData.get(0).setCount(outsideInvestModels.size());
            }
            setTabAdapter();
        });

//        binding.jyycRecyclerView.setOnScrollChangeListener((view, i, i1, i2, i3) -> {
//            int position = manager.findFirstVisibleItemPosition();
//            if (position != mPos) {
//                mPos = position;
//                LinearLayoutManager linearLayoutManager = (LinearLayoutManager) binding.recyclerView.getLayoutManager();
//                if (null != linearLayoutManager) {
//                    linearLayoutManager.scrollToPositionWithOffset(position, 0);
//                    for (CompanyLabelModel labelModel : tabData) {
//                        labelModel.setSelect(false);
//                    }
//                    tabData.get(position).setSelect(true);
//                    tabAdapter.notifyDataSetChanged();
//                }
//            }
//        });
    }

    private void setTabAdapter() {
//        tabAdapter = new InfoTabAdapter(context, tabData);
//        binding.recyclerView.setAdapter(tabAdapter);
//        tabAdapter.setOnTabItemClickListener(position -> {
//            for (CompanyLabelModel labelModel : tabData) {
//                labelModel.setSelect(false);
//            }
//            tabData.get(position).setSelect(true);
//            tabAdapter.notifyDataSetChanged();
//            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) binding.jyycRecyclerView.getLayoutManager();
//            if (null != linearLayoutManager) {
//                linearLayoutManager.scrollToPositionWithOffset(position, 0);
//            }
//        });
        OutsideInvestAdapter outsideInvestAdapter = new OutsideInvestAdapter(context, data);
        binding.jyycRecyclerView.setAdapter(outsideInvestAdapter);
    }
}